package com.wlyuan.open.scanner.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlyuan.open.scanner.dao.entity.OrderContainsDO;
import com.wlyuan.open.scanner.dao.entity.OrderSignLogDO;
import com.wlyuan.open.scanner.dao.mapper.OrderContainsMapper;
import com.wlyuan.open.scanner.dao.mapper.OrderSignLogMapper;
import com.wlyuan.open.scanner.repository.OrderSignLogRepository;
import com.wlyuan.open.scanner.utils.StreamUtils;
import com.wlyuan.open.scanner.utils.enums.InvalidEnum;
import com.wlyuan.open.scanner.utils.enums.order.OrderSrcClassEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.stream.Collectors;

/**
 * @author lijing
 * @description
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderSignLogRepositoryImpl implements OrderSignLogRepository {
    private final OrderSignLogMapper orderSignLogMapper;
    private final OrderContainsMapper orderContainsMapper;

    @Override
    public Integer getCertByOrder(Long orderId, Long rootOrderId, Long srcClass) {
        //根据订单id查询是否是拼车主单，如果是拼车主单，则分别查子单的原始单，只要有一单是有场景证书的，则返回有场景证书的。
        //不是拼车单，查询原始订单
        if (!OrderSrcClassEnum.isCombine(srcClass)) {
            var signLog = getOrderSignLogMap(rootOrderId);
            //不存在签收记录或者证书url为空
            if (signLog == null || StringUtils.isBlank(signLog.getCertUrl())) {
                return InvalidEnum.ENABLE.getValue();
            }
            return InvalidEnum.DISABLE.getValue();
        }
        //拼车单需要查询所有子订单
        QueryWrapper<OrderContainsDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrderContainsDO::getContainsOwnerorderid, orderId).eq(OrderContainsDO::getContainsInvalid, InvalidEnum.ENABLE.getValue());
        //所有子订单
        var list = orderContainsMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return InvalidEnum.ENABLE.getValue();
        }
        var orderIds = list.stream().map(OrderContainsDO::getContainsOrderid).collect(Collectors.toList());
        //子订单证书url
        var certUrlList = orderSignLogMapper.getCertUrlByOrderIds(orderIds);
        if (CollectionUtils.isEmpty(certUrlList)) {
            return InvalidEnum.ENABLE.getValue();
        }
        //当证书数量大于0 时，则存在证书
        long certUrlCount = certUrlList.stream().filter(orderSignLog -> StringUtils.isNotBlank(orderSignLog.getCertUrl())).count();
        if (certUrlCount > 0) {
            return InvalidEnum.DISABLE.getValue();
        }
        return InvalidEnum.ENABLE.getValue();
    }

    private OrderSignLogDO getOrderSignLogMap(Long orderId) {
        QueryWrapper<OrderSignLogDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrderSignLogDO::getRootOrderId, orderId).eq(OrderSignLogDO::getIsValid, InvalidEnum.ENABLE.getValue());
        var list = orderSignLogMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        //根据原始单分组 取第一个
        return StreamUtils.min(list, Comparator.comparing(OrderSignLogDO::getId));
    }

}
